package com.lz.yz.base.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.util.HtmlUtils;

/**
 * Created by Administrator on 2017/1/11 0011.
 */
public class EscapeUtil {
    private static final Log log = LogFactory.getLog(EscapeUtil.class);

    public EscapeUtil() {
    }

    public static String stripHeaderXSS(String value) {
        String ret = StringUtils.replace(value, "\r\n", "");
        ret = StringUtils.replace(ret, "<", "&lt;");
        ret = StringUtils.replace(ret, ">", "&gt;");
        return ret;
    }

    public static String stripXSS(String value) {
        if(value == null) {
            return null;
        } else {
            String ret = HtmlUtils.htmlEscape(value);
            return ret;
        }
    }

    public static String stripControlChar(String value) {
        if(value == null) {
            return null;
        } else {
            for(char deleteChar = 0; deleteChar <= 31; ++deleteChar) {
                if(deleteChar != 9 && deleteChar != 10 && deleteChar != 13) {
                    value = StringUtils.remove(value, deleteChar);
                }
            }

            char var2 = 127;
            value = StringUtils.remove(value, var2);
            return value;
        }
    }

    public static void main(String[] args) {
        char deleteChar = 20;
        char deleteChar1 = 0;
        String value = "<s" + deleteChar + "cript " + deleteChar1 + " src=\"abc.js\">alert();</script>杭州江干区城118号ABC, \"   hud \'  \'  1=1 or \r\n YYY ";
        String ret = null;
        System.out.println(stripControlChar(value));
        ret = HtmlUtils.htmlEscape(value);
        if(log.isDebugEnabled()) {
            log.debug(ret);
        }

        ret = stripXSS(value);
        if(log.isDebugEnabled()) {
            log.debug(ret);
        }

        ret = stripHeaderXSS(value);
        if(log.isDebugEnabled()) {
            log.debug(ret);
        }

    }
}
